package com.jdk21.core.controller;

import com.google.common.collect.Maps;
import com.jdk21.api.common.Response;
import com.jdk21.core.manager.FileManager;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/file")
public class FileController {

    @Resource
    private FileManager fileManager;

    @PostMapping("/upload")
    public Object upload(HttpServletRequest httpServletRequest, MultipartFile file) throws Exception {

        Map<String, Object> resultMap = Maps.newHashMap();
        resultMap.put("errno", 0);

        Map<String, Object> dataMap = Maps.newHashMap();
        resultMap.put("data", dataMap);

        String fileId = fileManager.uploadFile(file);

        resultMap.put("fileId", fileId);

        return Response.successResult(resultMap);
    }
}
